package sk.stuba.fiit.repka.dp.owl.gui;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;

import sk.stuba.fiit.repka.dp.gui.SelectFromCheckBoxesSP;
import sk.stuba.fiit.repka.dp.owl.MyOWLClass;

public class SelectDomainDialog extends JDialog implements ActionListener{
	
	private List<MyOWLClass> classes; //input to dialog
	
	private List<MyOWLClass> getClassesList() {
		if(this.classes == null) {
			this.classes = new ArrayList<MyOWLClass>();
		}
		return this.classes;
	}
	
	private List<MyOWLClass> outputClasses; //output of dialog
	
	public List<MyOWLClass> getOutputClasses() {
		if(this.outputClasses == null) {
			this.outputClasses = new ArrayList<MyOWLClass>();
		}
		return this.outputClasses;
	}
	
	private final JPanel contentPanel = new JPanel();
	private JPanel panel;
	private SelectFromCheckBoxesSP selectFromSP;
	
	/**
	 * Create the dialog.
	 */
	public SelectDomainDialog(List<MyOWLClass> classes) {
		this.classes = new ArrayList<MyOWLClass>(classes); //create list ... changes in new list should not have affect in old list!!!
		this.setModal(true);
		setTitle("Select domain");
		setBounds(100, 100, 450, 300);
		getContentPane().setLayout(new BorderLayout());
		contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
		getContentPane().add(contentPanel, BorderLayout.CENTER);
		contentPanel.setLayout(new GridLayout(1, 0, 0, 0));
		{
			selectFromSP = new SelectFromCheckBoxesSP(this.getClassesList());
			contentPanel.add(selectFromSP);
		}
		{
			JPanel buttonPane = new JPanel();
			buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT));
			getContentPane().add(buttonPane, BorderLayout.SOUTH);
			{
				JButton okButton = new JButton("OK");
				okButton.setActionCommand("OK");
				okButton.addActionListener(this);
				buttonPane.add(okButton);
				getRootPane().setDefaultButton(okButton);
			}
			{
				JButton cancelButton = new JButton("Cancel");
				cancelButton.setActionCommand("Cancel");
				cancelButton.addActionListener(this);
				buttonPane.add(cancelButton);
			}
		}
		this.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
		this.setVisible(true);
	}
	
	@Override
	public void actionPerformed(ActionEvent e) {
		if("OK".equals(e.getActionCommand())) {
			List<MyOWLClass> selectedClasses = new ArrayList<MyOWLClass>(selectFromSP.getSelectedOWLClassesFromCheckBoxes()); //get all selected classes
			this.outputClasses = new ArrayList<MyOWLClass>(selectedClasses); //output 
			this.dispose();
		} else if("Cancel".equals(e.getActionCommand())) {
			this.dispose();
		}
	}

}
